package scu.maqiang.numeric;

import static java.lang.Math.abs;


/**
 * 参数检查类, 验证所输入的参数是否有误
 * @author 马强
 *
 */
public class ParamCheck
{
    /**
     * 检查下标是否在弄个范围之内
     * @param idx 要检查的下标
     * @param low 下标的下界(可以达到)
     * @param upp 下标的上界(不可以达到)
     */
    public static void checkBounds(int idx, int low, int upp) {
        if (idx < low || idx >= upp) {
            throw new IndexOutOfBoundsException("Index Out Of Bounds: " + idx + ", not in (" + low + ", " + upp + ")");
        }
    }

    /**
     * 检查两个整数值是否相同, 用于维数的检查
     * @param i 整数i
     * @param j 整数j
     */
    public static void checkEqual(int i, int j) {
        if (i != j) {
            throw new IllegalArgumentException("Dimensions Dismatch: " + i + " does not equal to " + j);
        }
    }
    
    public static void isPositive(double x) {
    	if (x < 0 || abs(x) < Constants.Er) {
    		throw new IllegalArgumentException("The paramater must be positive!");
    	}
    }
    
    
    public static void isPositive(int x) {
    	if (x < 0) {
    		throw new IllegalArgumentException("The paramater must be positive!");
    	}
    }
    
    public static void isNonnegative(int x) {
    	if (x < 0) {
    		throw new IllegalArgumentException("The paramater must be Nonnegative!");
    	}
    }
}
